#if !USE_CONSOLE

#include "src/interface/lightDialog.h"

#include <QtGui>
#include <iostream>

#include "src/math/vector3d.h"

#include "src/defines/light.h"

#include "src/scene/areaLight.h"
#include "src/scene/pointLight.h"

LightDialog::LightDialog(int _id, unsigned int _type, QWidget *_parent)
    : QDialog(_parent), id(_id), type(_type){

    switch(type)
    {
        case AREA_LIGHT:
            setWindowTitle(QString::fromUtf8("Nova luz em área"));
            positionX = positionY = -0.5;
            positionZ = 4;
            bX = aY = 1;
            aX = aZ = bY = bZ = 0;
            break;
        case POINT_LIGHT:
            setWindowTitle(QString::fromUtf8("Nova luz pontual"));

            positionX = positionY = 0;
            positionZ = 4;
            aX = aY = aZ = bX = bY = bZ = 0;
            break;
    }


    light = NULL;
    colorR = colorG = colorB = 255;
    energy = 1;

    createLayout();
}

LightDialog::LightDialog(Light *_light, LightTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), light(_light), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar luz ").append(QString::number(_light->getId())));

    const Color &color = light->getColor();
    colorR = color.red * 255;
    colorG = color.green * 255;
    colorB = color.blue * 255;

    energy = light->getEnergy();

    type = light->getType();

    if(type == AREA_LIGHT)
    {

        const Vector3D &position = ((AreaLight *)light)->getPosition();
        positionX = position.x;
        positionY = position.y;
        positionZ = position.z;


        const Vector3D &a = ((AreaLight *)light)->getA();
        const Vector3D &b = ((AreaLight *)light)->getB();
        aX = a.x;
        aY = a.y;
        aZ = a.z;
        bX = b.x;
        bY = b.y;
        bZ = b.z;
    }else{
        const Vector3D &position = ((PointLight *)light)->getPosition();
        positionX = position.x;
        positionY = position.y;
        positionZ = position.z;

        aX = aY = aZ = bX = bY = bZ = 0;
    }

    createLayout();
}

LightDialog::~LightDialog(){
    delete colorPushButton;
}

void LightDialog::createLayout(){
    colorPushButton = new QPushButton(tr("Cor"));
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));

    setButtonColor();

    QLabel *energyLabel = new QLabel(QString::fromUtf8("Energia: "));
    QDoubleSpinBox *energyDoubleSpinBox = new QDoubleSpinBox;
    energyDoubleSpinBox->setMinimum(1);
    energyDoubleSpinBox->setMaximum(100000000);
    energyDoubleSpinBox->setValue(energy);
    connect(energyDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeEnergy(double)));

    QHBoxLayout *energyLayout = new QHBoxLayout;
    energyLayout->addWidget(energyLabel);
    energyLayout->addWidget(energyDoubleSpinBox);

    QVBoxLayout *colorLayout = new QVBoxLayout;
    colorLayout->addWidget(colorPushButton);
    colorLayout->addLayout(energyLayout);

    QGroupBox *colorGroupBox = new QGroupBox(QString::fromUtf8("Cor da luz"));
    colorGroupBox->setLayout(colorLayout);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(colorGroupBox);
    switch(type)
    {
        case AREA_LIGHT:
        {

            //------------------------------------------------------------A
            QLabel *aXLabel = new QLabel(tr("x:"));

            QDoubleSpinBox *aXDoubleSpinBox = new QDoubleSpinBox();
            aXDoubleSpinBox->setRange(-50.0, 50.0);
            aXDoubleSpinBox->setSingleStep(1.0);
            aXDoubleSpinBox->setValue(aX);
            connect(aXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeAX(double)));


            QLabel *aYLabel = new QLabel(tr("y:"));

            QDoubleSpinBox *aYDoubleSpinBox = new QDoubleSpinBox();
            aYDoubleSpinBox->setRange(-50.0, 50.0);
            aYDoubleSpinBox->setSingleStep(1.0);
            aYDoubleSpinBox->setValue(aY);
            connect(aYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeAY(double)));


            QLabel *aZLabel = new QLabel(tr("z:"));

            QDoubleSpinBox *aZDoubleSpinBox = new QDoubleSpinBox();
            aZDoubleSpinBox->setRange(-50.0, 50.0);
            aZDoubleSpinBox->setSingleStep(1.0);
            aZDoubleSpinBox->setValue(aZ);
            connect(aZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeAZ(double)));


            QHBoxLayout *aLayout = new QHBoxLayout;
            aLayout->addWidget(aXLabel);
            aLayout->addWidget(aXDoubleSpinBox);
            aLayout->addStretch(1);
            aLayout->addWidget(aYLabel);
            aLayout->addWidget(aYDoubleSpinBox);
            aLayout->addStretch(1);
            aLayout->addWidget(aZLabel);
            aLayout->addWidget(aZDoubleSpinBox);
            aLayout->addStretch(1);

            QGroupBox *aGroupBox = new QGroupBox(QString::fromUtf8("A (vetor)"));
            aGroupBox->setLayout(aLayout);

            mainLayout->addWidget(aGroupBox);


            //------------------------------------------------------------B
            QLabel *bXLabel = new QLabel(tr("x:"));

            QDoubleSpinBox *bXDoubleSpinBox = new QDoubleSpinBox();
            bXDoubleSpinBox->setRange(-50.0, 50.0);
            bXDoubleSpinBox->setSingleStep(1.0);
            bXDoubleSpinBox->setValue(bX);
            connect(bXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeBX(double)));


            QLabel *bYLabel = new QLabel(tr("y:"));

            QDoubleSpinBox *bYDoubleSpinBox = new QDoubleSpinBox();
            bYDoubleSpinBox->setRange(-50.0, 50.0);
            bYDoubleSpinBox->setSingleStep(1.0);
            bYDoubleSpinBox->setValue(bY);
            connect(bYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeBY(double)));


            QLabel *bZLabel = new QLabel(tr("z:"));

            QDoubleSpinBox *bZDoubleSpinBox = new QDoubleSpinBox();
            bZDoubleSpinBox->setRange(-50.0, 50.0);
            bZDoubleSpinBox->setSingleStep(1.0);
            bZDoubleSpinBox->setValue(bZ);
            connect(bZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeBZ(double)));


            QHBoxLayout *bLayout = new QHBoxLayout;
            bLayout->addWidget(bXLabel);
            bLayout->addWidget(bXDoubleSpinBox);
            bLayout->addStretch(1);
            bLayout->addWidget(bYLabel);
            bLayout->addWidget(bYDoubleSpinBox);
            bLayout->addStretch(1);
            bLayout->addWidget(bZLabel);
            bLayout->addWidget(bZDoubleSpinBox);
            bLayout->addStretch(1);

            QGroupBox *bGroupBox = new QGroupBox(QString::fromUtf8("B (vetor)"));
            bGroupBox->setLayout(bLayout);

            mainLayout->addWidget(bGroupBox);

            //esse aqui nao tem break mesmo!
        }
        case POINT_LIGHT:
        {

            //------------------------------------------------------------Position
            QLabel *positionXLabel = new QLabel(tr("x:"));

            QDoubleSpinBox *positionXDoubleSpinBox = new QDoubleSpinBox();
            positionXDoubleSpinBox->setRange(-50.0, 50.0);
            positionXDoubleSpinBox->setSingleStep(1.0);
            positionXDoubleSpinBox->setValue(positionX);
            connect(positionXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changePositionX(double)));


            QLabel *positionYLabel = new QLabel(tr("y:"));

            QDoubleSpinBox *positionYDoubleSpinBox = new QDoubleSpinBox();
            positionYDoubleSpinBox->setRange(-50.0, 50.0);
            positionYDoubleSpinBox->setSingleStep(1.0);
            positionYDoubleSpinBox->setValue(positionY);
            connect(positionYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changePositionY(double)));


            QLabel *positionZLabel = new QLabel(tr("z:"));

            QDoubleSpinBox *positionZDoubleSpinBox = new QDoubleSpinBox();
            positionZDoubleSpinBox->setRange(-50.0, 50.0);
            positionZDoubleSpinBox->setSingleStep(1.0);
            positionZDoubleSpinBox->setValue(positionZ);
            connect(positionZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changePositionZ(double)));


            QHBoxLayout *positionLayout = new QHBoxLayout;
            positionLayout->addWidget(positionXLabel);
            positionLayout->addWidget(positionXDoubleSpinBox);
            positionLayout->addStretch(1);
            positionLayout->addWidget(positionYLabel);
            positionLayout->addWidget(positionYDoubleSpinBox);
            positionLayout->addStretch(1);
            positionLayout->addWidget(positionZLabel);
            positionLayout->addWidget(positionZDoubleSpinBox);
            positionLayout->addStretch(1);

            QGroupBox *positionGroupBox = new QGroupBox(QString::fromUtf8("Posição (ponto)"));
            positionGroupBox->setLayout(positionLayout);

            mainLayout->addWidget(positionGroupBox);
            break;
        }
    }



    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void LightDialog::setButtonColor(){
    QString colorStr;
    if(colorR + colorB + colorG < 400){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
        colorStr.append(QString::fromUtf8("border: 1px solid white;"));
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
        colorStr.append(QString::fromUtf8("border: 1px solid black;"));
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
//    colorStr.append(QString::fromUtf8(", "));
//    colorStr.append(QString::number(colorA));
    colorStr.append(QString::fromUtf8(");"));
    colorStr.append(QString::fromUtf8("border-radius: 8px;"));
    colorStr.append(QString::fromUtf8("height: 20px;"));

    colorPushButton->setStyleSheet(colorStr);

}

void LightDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB/*, colorA*/), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
//        colorA = color.alpha();
    }
    setButtonColor();
}


void LightDialog::acceptAndCreate(){
    if(!light){
        switch(type)
        {
            case AREA_LIGHT:
                light = new AreaLight(id, Vector3D(positionX, positionY, positionZ), Vector3D(aX, aY, aZ), Vector3D(bX, bY, bZ), Color(colorR/255.0, colorG/255.0, colorB/255.0));
                break;
            case POINT_LIGHT:
                light = new PointLight(id, Vector3D(positionX, positionY, positionZ), Color(colorR/255.0, colorG/255.0, colorB/255.0));
                break;
        }
        done(light);
    }
    else
    {
        light->setColor(Color(colorR/255.0, colorG/255.0, colorB/255.0));
        switch(type)
        {
            case AREA_LIGHT:
                ((AreaLight *)light)->setPosition(Vector3D(positionX, positionY, positionZ));
                ((AreaLight *)light)->setA(Vector3D(aX, aY, aZ));
                ((AreaLight *)light)->setB(Vector3D(bX, bY, bZ));
                break;
            case POINT_LIGHT:
                ((PointLight *)light)->setPosition(Vector3D(positionX, positionY, positionZ));
                break;
        }
    }

    accept();
    done();
}

#endif //!USE_CONSOLE
